<template>
  <el-dialog title="部门查询" :close-on-click-modal="false" :visible.sync="visible" fullscreen append-to-body center>
    <el-button class="miui-dialog-close-btn" @click="visible = false">关闭页面</el-button>
    <div class="text item">
      <el-form inline size="mini">
        <el-form-item label="部门编码">
          <el-input v-model="dataForm.code"></el-input>
        </el-form-item>
        <el-form-item label="部门名称">
          <el-input v-model="dataForm.name"></el-input>
        </el-form-item>
        <el-form-item label="部门状态">
          <el-select v-model="dataForm.status">
            <el-option value="1" label="启用"></el-option>
            <el-option value="0" label="停用"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="部门类型">
          <el-select v-model="dataForm.deptType" disabled>
            <el-option value="1" label="销售门店"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所属公司">
          <el-select v-model="dataForm.unitId">
            <el-option v-for='item in companyOption' :value="item.id" :label="item.name" :key="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-search" type="primary" @click="getDataListt">查询</el-button>
          <el-button icon="el-icon-delete" type="info" @click="clearSearchForm">清空</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-row style="margin-bottom: 3px;">
      <el-button type="primary" size="mini" @click="choiceAndClose">选择并关闭</el-button>
    </el-row>
    <el-table :data="dataList" height="calc(100vh - 215px)" ref="singleTable" border v-loading="dataListLoading" @selection-change="handleSelectionChange">
      <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
      <el-table-column prop="code" header-align="center" align="center" label="部门编码"></el-table-column>
      <el-table-column prop="name" header-align="center" align="center" label="部门名称"></el-table-column>
      <el-table-column prop="deptType" header-align="center" align="center" label="部门类型">
        <template slot-scope="scope"> {{typeTurn(scope.row.deptType)}} </template>
      </el-table-column>
      <el-table-column prop="unitName" header-align="center" align="center" label="所属公司"></el-table-column>
    </el-table>
    <div class="avue-crud__pagination">
      <el-pagination
        @size-change="sizeChangeHandle"
        @current-change="currentChangeHandle"
        :current-page="pageIndex"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="pageSize"
        :total="totalPage"
        background
        layout="total, sizes, prev, pager, next">
      </el-pagination>
    </div>
  </el-dialog>
</template>

<script>
  import {filterParams} from '@/util/tools'
  import { unitList } from '@/api/admin/unit'
  import { getDeptPage } from '@/api/admin/dept'
  export default {
    props: ['company'],
    data () {
      return {
        visible: false,
        companyOption: [],
        selectionList: [],
        dataForm: {
          supplyId: '',
          supplyCode: '',
          supplyName: '',
          ctgId: '',
          status: ''
        },
        dataList: [],
        dataListLoading: false,
        pageIndex: 1,
        pageSize: 20,
        totalPage: 0
      }
    },
    methods: {
      init() {
        this.visible = true
        this.dataForm.unitId = this.company
        // for(let key in this.dataForm){
        //   this.dataForm[key] = ''
        // }
        // this.dataForm.ctgId = data.ctgId
        // this.dataForm.supplyId = data.supplyId
        this.dataForm.deptType = '1'
        this.getDataListt()
        this.getCompanyList()
      },
			clearSearchForm() {
				for(let key in this.dataForm){
					this.dataForm[key] = ''
				}
			},
      getDataListt() {
        let params1 = {current: this.pageIndex,size: this.pageSize}
        let params2 = filterParams(this.dataForm)
        getDeptPage(Object.assign(params1,params2)).then(result => {
          this.dataList = result.data.data.records
          this.totalPage = result.data.data.total
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataListt()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataListt()
      },
      //  获取公司列表
      getCompanyList() {
        unitList().then(result => {
          this.companyOption = result.data.data
        })
      },
      // 选择品种
      checkitem(val) {
        this.visible = false
        this.$emit('refreshData',val)
      },
      //  门店类型转换
      typeTurn(type) {
        switch (type){
          case 1:
            return '销售门店'
            break
          case 2:
            return '加工中心'
            break
          case 3:
            return '总库'
            break
        }
      },
      //  多选表格改变事件
      handleSelectionChange(val) {
        this.selectionList = val
      },
      //  选择部门并关闭弹窗
      choiceAndClose() {
        this.visible = false
        this.$emit('refreshData',this.selectionList)
      }
    }
  }
</script>

<style>
</style>
